package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.OrderService;
import org.csu.mypetstore.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ViewOrderListServlet extends HttpServlet {
    private static final String LIST_ORDER = "/WEB-INF/jsp/order/ListOrder.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.removeAttribute("paySuccess");

        List<Order> orderList = new ArrayList<>();
        OrderService orderService = new OrderServiceImpl();
        orderList = orderService.getOrdersByUsername(((Account)req.getSession().getAttribute("account")).getUsername());
        req.getSession().setAttribute("orderList", orderList);
        req.getRequestDispatcher(LIST_ORDER).forward(req, resp);
    }
}
